package com.example.qrcode.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.qrcode.entity.GraphicsLibrary;
import com.example.qrcode.mapper.GraphicsLibraryMapper;
import org.springframework.stereotype.Service;

@Service
public class GraphicsLibraryService extends ServiceImpl<GraphicsLibraryMapper, GraphicsLibrary> {

    public String getSvgById(Long id) {
        return this.baseMapper.getSvgById(id);
    }
    
    /**
     * 根据出线数量获取对应的SVG图形
     * @param lineNum 出线数量
     * @return SVG内容
     */
    public String getSvgByLineNum(Integer lineNum) {
        // 如果lineNum为null或0，默认使用8出线
        if (lineNum == null || lineNum <= 0) {
            lineNum = 8;
        }
        
        // 限制最大出线数为8
        if (lineNum > 8) {
            lineNum = 8;
        }
        
        // 构造graphic_name，格式：1出线M, 2出线M, ..., 8出线M
        String graphicName = lineNum + "出线M";
        
        System.out.println("=== 主接线图出线数量判断 ===");
        System.out.println("原始line_num: " + lineNum);
        System.out.println("查询的graphic_name: " + graphicName);
        
        // 调用mapper查询对应的SVG
        String svg = this.baseMapper.getSvgByGraphicName(graphicName);
        
        if (svg == null || svg.isEmpty()) {
            System.out.println("未找到对应的SVG图形: " + graphicName);
            // 如果没找到，尝试查询8出线M作为默认值
            if (lineNum != 8) {
                System.out.println("尝试加载默认的8出线M图形");
                svg = this.baseMapper.getSvgByGraphicName("8出线M");
            }
        } else {
            System.out.println("成功找到SVG图形: " + graphicName);
        }
        
        return svg;
    }
}
